<?php  if (!defined('PATH_HD')) exit('No direct script access allowed');

class loginControl extends Control{

    //载入登录页面
    function index() {
        // C("debug",1);
        $this->display("login");
    }

    //取得验证码
    function getCode() {
        $code = new code();
        $code->show();
    }

    //接受表单里的数据并异步验证
    function checklogin(){
    	    C("debug",1);
            if (strtolower($_SESSION['code']) != strtolower($_POST['code'])) {
                    echo json_encode(array('status' => 0, 'code'=>'验证码错误'));
                    die;
             }
            $username = $_POST['name'];
            $pwd =$_POST['pwd'];
            $db=m("user");
            $row=$db->all();
            foreach ($row as $v) {
                if ($v['username'] == $username) {
                        if ($v['lock']) {
                            echo json_encode(array('status' => 4, 'yes' => '该用户已被锁定，请联系管理员'));
                                die;
                        }
                        if ($v['password'] == $pwd) {
                                $_SESSION['easyuser']=$_POST['name'];
                                // setcookie("easyuser",$_POST['name'],0,"/");
                                echo json_encode(array('status' => 1, 'yes' => '登录成功','url'=>$_SERVER["HTTP_REFERER"]));
                                die;
                        } else {
                                echo json_encode(array('status' => 2, 'pwd' => '密码错'));
                                die;
                        }
                }
            }
            echo json_encode(array('status' => 3, 'user' => '用户不存在'));
    }
    
   //显示注册界面
    function reg(){
      $this->display();
    }
    
   //注册提交上来的
    function addUser() {
        // p($_SERVER);die;
        if (!empty($_POST)) {
        	$_POST['username'] =$_POST['uname'];
        	$_POST['password'] =$_POST['pwd'];
            $_POST['email'] =$_POST['email'];
            $_POST['registime'] = $_SERVER['REQUEST_TIME'];
            $_POST['logintime'] = $_SERVER['REQUEST_TIME'];
            $_POST['regisip'] = $_SERVER['REMOTE_ADDR'];
            $_POST['loginip'] = $_SERVER['REMOTE_ADDR'];
            // p($_POST);die;
            $db = m('user');
            if ($db->add()) {
                go("index");
            }   
        }
    }
    

    //退出登录
    function logout(){
       // unset($_SESSION['user']);
        session_destroy();
        go("index/index");
    }


    
}
?>